//
//  KABMallSurOrdFooterHFView.m
//  kabu
//
//  Created by XiangDongMBP on 2017/2/21.
//  Copyright © 2017年 East. All rights reserved.
//

#import "KABMallSurOrdFooterHFView.h"
#import "GlobalHeader.h"
#import "UIColor+XDHex.h"
#import "KABModel.h"

@implementation KABMallSurOrdFooterHFView

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
    self.contentView.backgroundColor = [UIColor whiteColor];
    
    self.rightBtn.layer.masksToBounds = YES;
    self.rightBtn.layer.borderColor = [UIColor xd_colorWithHex:KABGlobalColorRedBtnBorderColor].CGColor;
    self.rightBtn.layer.cornerRadius = 4;
    self.rightBtn.layer.borderWidth = 1;
    [self.rightBtn addTarget:self action:@selector(rightBtnTapped:) forControlEvents:UIControlEventTouchUpInside];
    
    self.leftBtn.layer.masksToBounds = YES;
    self.leftBtn.layer.borderColor = [UIColor xd_colorWithHex:KABGlobalColorBtnBorderColor].CGColor;
    self.leftBtn.layer.cornerRadius = 4;
    self.leftBtn.layer.borderWidth = 1;
    [self.leftBtn addTarget:self action:@selector(leftBtnTapped:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)leftBtnTapped:(UIButton *)btn {
    if ([self.delegate respondsToSelector:@selector(kABMallSurOrdFooterHFView:section:leftBtnTapped:)]) {
        [self.delegate kABMallSurOrdFooterHFView:self section:self.section leftBtnTapped:btn];
    }
}

- (void)rightBtnTapped:(UIButton *)btn {
    if ([self.delegate respondsToSelector:@selector(kABMallSurOrdFooterHFView:section:rightBtnTapped:)]) {
        [self.delegate kABMallSurOrdFooterHFView:self section:self.section rightBtnTapped:btn];
    }
}

- (void)configViewWithOrderDetail:(KABMallOrderDetail *)orderDetail {
    
    NSString *str1 = [NSString stringWithFormat:@"共%zd件商品 合计：", orderDetail.total_number];
    NSString *price1 = [NSString stringWithFormat:@"￥%.2f", orderDetail.total_price / 100.0];
    NSString *totalStr = [NSString stringWithFormat:@"%@%@ (含运费￥%.2f)", str1, price1, orderDetail.freight / 100.0];
    NSMutableAttributedString *mAStr = [[NSMutableAttributedString alloc] initWithString:totalStr];
    NSDictionary *dic = @{NSForegroundColorAttributeName : [UIColor xd_colorWithHex:KABGlobalColorTabBarItemColor],
                          NSFontAttributeName : [UIFont systemFontOfSize:18.0]};
    [mAStr addAttributes:dic range:NSMakeRange(str1.length, price1.length)];
    self.totalLabel.attributedText = mAStr;
    
    // 待付款
    self.timeLabel.hidden = (orderDetail.order_type != 1);
    self.timeLabel.text = [NSString stringWithFormat:@"失效时间：%@", orderDetail.dead_time];
    
    // 退货
    if (orderDetail.order_type == 5) {
        self.timeLabel.hidden = NO;
        NSString *startTimeStr = [NSString stringWithFormat:@"申请退货时间：%@", orderDetail.refund_start_time];
        NSString *endTimeStr = [NSString stringWithFormat:@"退货结束时间：%@", orderDetail.refund_end_time];
        self.timeLabel.text = (orderDetail.refund_state == 0) ? startTimeStr : endTimeStr;
    }
    
    NSString *leftBtnTitle = nil;
    NSString *rightBtnTitle = nil;
    if (orderDetail.order_type == 1) {
        rightBtnTitle = @"付款";
    } else if (orderDetail.order_type == 2){
        rightBtnTitle = @"催促发货";
    } else if (orderDetail.order_type == 3){
        leftBtnTitle = @"查看物流";
        rightBtnTitle = @"确认收货";
    } else if (orderDetail.order_type == 4){
        leftBtnTitle = @"查看物流";
        rightBtnTitle = @"申请退货";
    } else if (orderDetail.order_type == 5) {
        switch (orderDetail.refund_state) {
            case 0:
                rightBtnTitle = @"退货中";
                break;
            case 1:
                rightBtnTitle = @"退货成功";
                break;
            case 2:
                rightBtnTitle = @"退货失败";
                break;
            default:
                break;
        }
    }
    self.leftBtn.hidden = !leftBtnTitle;
    self.rightBtn.hidden = !rightBtnTitle;
    [self.leftBtn setTitle:leftBtnTitle forState:UIControlStateNormal];
    [self.rightBtn setTitle:rightBtnTitle forState:UIControlStateNormal];
    self.rightBtn.userInteractionEnabled = (orderDetail.order_type != 5);
    self.rightBtn.layer.borderWidth = (orderDetail.order_type != 5) ? 1 : 0;
}

@end


@implementation KABMallSurOrdFooterHFView (MallSureOrder)
- (void)configViewWithAttributeString:(NSAttributedString *)aStr {
    self.totalLabel.attributedText = aStr;
    self.bottomBgViewHeightConstraint.constant = 0;
}
@end
